﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace TestApplication
{
    public partial class Form3 : zytech.common.baseform.sys.PermissionView
    {
        public Form3()
        {
            InitializeComponent();
            SqlConnStr = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            //pictureBox1.Image = TestApplication.Properties.Resources.logo;
            string sql = "select * from sys_user where cUserid != 'admin'";
            zytech.common.Functions.Bound(dataGridView1, sql, SqlConnStr);
            dataGridView1.Columns[0].HeaderText = "用户名";
            dataGridView1.Columns[1].HeaderText = "姓名";
            dataGridView1.Columns[2].HeaderText = "密码";
            dataGridView1.Columns[3].HeaderText = "是否停用";
            //加载父节点
            InitialFirstNode();
            //展开所有节点
            treeView1.ExpandAll();
            string id = dataGridView1.CurrentRow.Cells["cUserid"].Value.ToString();
            setright(treeView1.Nodes[0], id);
        }

        #region 取消选择节点时,当前节点的父节点所有节点皆未被选中时，取消当前节点父节点的选中状态
        protected virtual void setParentNodeFalse(TreeNode currNode)
        {
            TreeNode parentNode = currNode.Parent;
            TreeNode curr = parentNode.FirstNode;
            while (curr != parentNode.LastNode)        //从同级的第一个节点循环到最后一个节点
            {
                if (curr.Checked == true)
                    break;
                else
                    curr = curr.NextNode;
            }
            if (curr == parentNode.LastNode && parentNode.LastNode.Checked == false)
                parentNode.Checked = false;     //如果与该节点同级的节点都未被选中则取消其父节点的选中状态
            //递归
            if (parentNode.Parent != null)
            {
                curr = parentNode.Parent.FirstNode;
                while (curr != parentNode.Parent.LastNode)
                {
                    if (curr.Checked == true)
                        break;
                    else
                        curr = curr.NextNode;
                }
                if (curr == parentNode.Parent.LastNode && parentNode.Parent.LastNode.Checked == false)
                    setParentNodeFalse(parentNode);
            }
        }
        #endregion

        #region 选中子节点同时选中父节点
        protected virtual void setParentNodeCheckedState(TreeNode currNode, bool state)
        {
            TreeNode parentNode = currNode.Parent;
            parentNode.Checked = state;
            if (currNode.Parent.Parent != null)
            {
                setParentNodeCheckedState(currNode.Parent, state);
            }
        }
        #endregion

        #region 选中节点为树节点则选中所有子节点
        protected virtual void setChildNodeCheckedState(TreeNode currNode, bool state)
        {
            TreeNodeCollection nodes = currNode.Nodes;
            if (nodes.Count > 0)
                foreach (TreeNode tn in nodes)
                {
                    tn.Checked = state;
                    setChildNodeCheckedState(tn, state);
                }
        }
        #endregion
        protected override void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            /*
            if (e.Action == TreeViewAction.ByMouse)
            {
                if (e.Node.Checked)
                {
                    if (e.Node.Parent == null)
                        setChildNodeCheckedState(e.Node, true);      //选中当前节点的同时选中其所有子节点
                    else
                    {
                        //选中当前节点的同时选中其父节点
                        setParentNodeCheckedState(e.Node, true);
                        //选中当前节点的同时选中其所有子节点
                        setChildNodeCheckedState(e.Node, true);
                    }
                }
                else
                {
                    //取消父节点选中状态之后，取消所有子节点的选中状态
                    setChildNodeCheckedState(e.Node, false);
                    //取消一个树节点的所有子节点选中状态后，取消此树节点的选中状态
                    if (e.Node.Parent != null)
                    {
                        setParentNodeFalse(e.Node);
                    }
                }
            }*/
        }
    }
}
